package cn.iocoder.yudao.module.product.service.cylindertype;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.cylindertype.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.cylindertype.CylinderTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.cylindertype.CylinderTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 钢瓶类型 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CylinderTypeServiceImpl implements CylinderTypeService {

    @Resource
    private CylinderTypeMapper cylinderTypeMapper;

    @Override
    public Long createCylinderType(CylinderTypeSaveReqVO createReqVO) {
        // 插入
        CylinderTypeDO cylinderType = BeanUtils.toBean(createReqVO, CylinderTypeDO.class);
        cylinderType.setCreateTime(LocalDateTime.now());
        cylinderTypeMapper.insert(cylinderType);
        // 返回
        return cylinderType.getId();
    }

    @Override
    public void updateCylinderType(CylinderTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateCylinderTypeExists(updateReqVO.getId());
        // 更新
        CylinderTypeDO updateObj = BeanUtils.toBean(updateReqVO, CylinderTypeDO.class);
        updateObj.setUpdateTime(LocalDateTime.now());
        cylinderTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCylinderType(Long id) {
        // 校验存在
        validateCylinderTypeExists(id);
        // 删除
        cylinderTypeMapper.deleteById(id);
    }

    private void validateCylinderTypeExists(Long id) {
        if (cylinderTypeMapper.selectById(id) == null) {
            throw exception(CYLINDER_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public CylinderTypeDO getCylinderType(Long id) {
        return cylinderTypeMapper.selectById(id);
    }

    @Override
    public PageResult<CylinderTypeDO> getCylinderTypePage(CylinderTypePageReqVO pageReqVO) {
        return cylinderTypeMapper.selectPage(pageReqVO);
    }

    /**
     *  获取钢瓶类型列表
     * @param
     * @return
     */
    @Override
    public List<CylinderTypeDO> getCylinderTypeList() {
        List<CylinderTypeDO> list = cylinderTypeMapper.selectList();

        return list;
    }

}